package com.qingsong.controller;

import cn.hutool.core.bean.BeanUtil;
import com.qingsong.dto.*;
import com.qingsong.entity.Assets;
import com.qingsong.excel.AssetsExcel;
import com.qingsong.result.Result;
import com.qingsong.service.AssetsService;
import com.qingsong.util.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author qingsong
 */
@Tag(name="资产模块")
@RestController
@RequestMapping("/api/v1/assets")
public class AssetsController {
    @Autowired
    private AssetsService assetsService;

    @Operation(summary = "单增一条记录")
    @PostMapping("save")
    public Object save(@RequestBody @Validated AssetsInsertDTO assetsInsertDTO){
        return assetsService.insert(assetsInsertDTO);
    }

    @Operation(summary = "单查一条记录")
    @GetMapping("getInfo/{id}")
    public Object getInfo(@PathVariable Long id){
        return assetsService.selectById(id);
    }

    @Operation(summary = "修改一条记录")
    @PostMapping("update")
    public Object update(@RequestBody @Validated AssetsUpdateDTO assetsUpdateDTO){
        return assetsService.updateById(assetsUpdateDTO);
    }

    @Operation(summary = "删除一条记录")
    @PostMapping ("/remove/{id}")
    public Object remove(@PathVariable Long id){
        return assetsService.logicDeleteById(id);
    }

    @Operation(summary = "批量删除多条记录")
    @PostMapping ("removeBatch")
    public Object removeBatch(@RequestParam List<Long> ids){
        return assetsService.logicDeleteByIds(ids);
    }

    @Operation(summary = "查询全部记录")
    @GetMapping ("list")
    public Object list(){
        return assetsService.list();
    }

    @Operation(summary = "分页查询记录")
    @PostMapping ("page")
    public Object page(@RequestBody @Validated PageDTO pageDTO){
        return assetsService.page(pageDTO);
    }

    @Operation(summary = "打印数据报表")
    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp) {
        List<AssetsExcel> assetsExcels = new ArrayList<>();
        List<Assets> assetss = assetsService.list();
        for (Assets assets : assetss) {
            AssetsExcel assetsExcel = BeanUtil.copyProperties(assets, AssetsExcel.class);
            assetsExcels.add(assetsExcel);
        }
        EasyExcelUtil.download(resp, "资产统计表.xlsx", "sheet01",
                AssetsExcel.class, assetsExcels);
    }

    @Operation(summary = "按资产名搜索")
    @PostMapping("pageLikeTitle")
    public Object pageLikeTitle(@RequestBody @Validated AssetsPageDTO dto){
        return assetsService.pageLikeTitle(dto);
    }

    //上传文件不是JSON参数,而是二进制参数,不能用 @RequestBody
    @Operation(summary = "上传资产图片")
    @PostMapping("uploadPicture")
    public Object uploadPicture(AssetsUploadDTO dto){
        return new Result<>(assetsService.uploadPicture(dto));
    }
}
